package com.ksd.modules.rbac.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ksd.common.enums.DeletedEnums;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.convert.CopyConvertUtils;
import com.ksd.modules.rbac.api.entity.SysApis;
import com.ksd.modules.rbac.api.mapper.SysApisMapper;
import com.ksd.modules.rbac.api.po.SysApisSavePO;
import com.ksd.modules.rbac.api.po.SysApisSearchPO;
import com.ksd.modules.rbac.api.service.ISysApisService;
import com.ksd.modules.system.enums.DefaultEnums;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author Auto Generator
 * @since 2023-08-13
 */
@Service
public class SysApisService extends BaseServiceImpl<SysApisMapper, SysApis> implements ISysApisService {

    /**
     * 查询菜单接口搜索并分页
     * @return
     */
    @Override
    public List<SysApis> findApisRoot(){
        LambdaQueryWrapper<SysApis> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysApis::getIsDeleted, DeletedEnums.UN_DELETED.getCode());
        lambdaQueryWrapper.eq(SysApis::getParentId, DefaultEnums.NO.getCode());
        lambdaQueryWrapper.orderByAsc(SysApis::getSort);
        return this.list(lambdaQueryWrapper);
    }

    /**
     * 查询菜单接口搜索并分页
     * @return
     */
    @Override
    public List<SysApis> findApis(SysApisSearchPO sysApisSearchPO){
        LambdaQueryWrapper<SysApis> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(sysApisSearchPO.getKeyword())) {
            lambdaQueryWrapper.and(c -> c.like(SysApis::getTitle, sysApisSearchPO.getKeyword())
                    .or().like(SysApis::getTitle, sysApisSearchPO.getKeyword())
                    .or().like(SysApis::getPath, sysApisSearchPO.getKeyword()));
        }
        lambdaQueryWrapper.eq(SysApis::getIsDeleted,DeletedEnums.UN_DELETED.getCode());
        lambdaQueryWrapper.eq(Objects.nonNull(sysApisSearchPO.getStatus()), SysApis::getStatus,sysApisSearchPO.getStatus());
        lambdaQueryWrapper.orderByAsc(SysApis::getSort);
        //数据查询
        List<SysApis> list = this.list(lambdaQueryWrapper);
        //新建一个用于接收数据的list
        List<SysApis> resultList = new ArrayList<>();
        for (SysApis result : list) {
            if (result.getParentId() == 0L) {
                //调用方法给子类添加数据
                resultList.add(getApisTree(result, list));
            }
        }
        return resultList;
    }

    private SysApis getApisTree(SysApis result, List<SysApis> list) {
        for (SysApis apis : list) {
            //如果父类主键等于传过来实体类的ID
            if (Objects.equals(apis.getParentId(), result.getId())) {
                if (result.getChildren() == null) {
                    result.setChildren(new ArrayList<>());
                }
                // 递归调用
                result.getChildren().add(getApisTree(apis, list));
            }
        }
        return result;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveUpdateSysApis(SysApisSavePO adminMayaAdvertsSavePO) {
        SysApis mayaSysApis = CopyConvertUtils.copyProperties(adminMayaAdvertsSavePO, SysApis.class);
        return this.saveOrUpdate(mayaSysApis);
    }

    @Override
    public boolean deleteSysApisById(Long id) {
        return this.removeById(id);
    }

    @Override
    public SysApis getSysApisById(Long id) {
        return this.getById(id);
    }
}
